package com.arpa.wms.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.cache.PartyCache;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.domain.dto.MoveRegisterDTO;
import com.arpa.wms.domain.entity.MoveRegister;
import com.arpa.wms.domain.vo.MoveRegisterVO;
import com.arpa.wms.mapper.MoveRegisterMapper;
import com.arpa.wms.service.IMoveRegisterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author: Allen chen
 * @Date 2020/11/4
 * @Description: 负数-减操作，正数-加操作
 */
@Service
public class MoveRegisterServiceImpl extends ServiceImpl<MoveRegisterMapper, MoveRegister> implements IMoveRegisterService{
    @Autowired
    private PartyCache partyCache;

    @Override
    public List<MoveRegisterVO> queryByCondition(MoveRegisterDTO moveRegisterDTO) {
        moveRegisterDTO.setGroupCode(UserUtil.getBranchCode());
        return baseMapper.queryByCondition(moveRegisterDTO);
    }

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(MoveRegister entity) {
        if(StringUtils.isBlank(entity.getCode())){
            entity.setCode(IdUtil.simpleUUID());
        }
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
            entity.setCreatedName(partyCache.translate(UserUtil.getCode()));
        }
        return super.save(entity);
    }

    @Override
    public List<MoveRegisterVO> queryByItemCodes(String itemCodes) {

        return baseMapper.queryByItemCodes(itemCodes);
    }

    @Deprecated
    @Override
    public List<MoveRegisterVO> queryByStatus(MoveRegisterDTO moveRegisterDTO) {
        return baseMapper.queryByStatus(moveRegisterDTO);
    }

    @Override
    public int updateQuantity(MoveRegisterDTO moveRegisterDTO) {
        return baseMapper.updateQuantity(moveRegisterDTO);
    }

    @Override
    public int updateiMovedQuantity(MoveRegisterDTO moveRegisterDTO) {
        return baseMapper.updateiMovedQuantity(moveRegisterDTO);
    }
}
